home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / cops / cops_104 / src / crc_check.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-10  |  4.2 KB  |  198 lines

  1. /*
  2.     This progam will compare two crc lists and report the differences.
  3.     
  4.     By Jon Zeeff (zeeff@b-tech.ann-arbor.mi.us)
  5.  
  6.     Permission is granted to use this in any manner provided that    
  7.     1) the copyright notice is left intact, 
  8.     2) you don't hold me responsible for any bugs and 
  9.     3) you mail me any improvements that you make.  
  10.  
  11.  
  12.     report:
  13.          corrupt    -    crc changed w/o date change
  14.          replaced    -    crc + date changed
  15.          perm        -    permissions changed
  16.          own/grp    -    owner or group changed
  17.      removed    -    
  18.      added        -
  19.  
  20. Print the info for the new file except for deleted.
  21.  
  22. Use:
  23.  
  24. find / -print | sort | xargs crc -v > crc_file
  25.  
  26. to generate a crc list (crc.c should accompany this source).
  27.  
  28. Assume that no files have tabs or spaces in the name.
  29.  
  30. */
  31.  
  32. /*
  33.  sequent stuff -- may or may not need it?  Worked fine without it
  34. on a sequent I had, but others claim they need it.  Go figure.
  35.  
  36. #ifdef sequent
  37. #define strrchr(s, c)    rindex(s,c)
  38. #endif
  39.  
  40. */
  41.  
  42. /* max size of line */
  43.  
  44. #define BUF_SIZE 1124
  45.  
  46. #include <stdio.h>
  47.  
  48. char    *strrchr();
  49. void    exit();
  50.  
  51. char    new_line[BUF_SIZE];
  52. char    old_line[BUF_SIZE];
  53.  
  54. FILE *new_file;
  55. FILE *old_file;
  56.  
  57. main(argc, argv)
  58. int    argc;
  59. char    **argv;
  60. {
  61.    /*
  62.  
  63.            If line =, read new line from each file
  64.            else
  65.            If date/perm/crc change, report and read new line from each file
  66.            else
  67.            If old_line < new_line, report file removed, read old line
  68.            else
  69.               report new line as added
  70.               read new_line
  71.         loop
  72. */
  73.  
  74.    char    *new_ptr;
  75.    char    *old_ptr;
  76.  
  77.    if (argc != 3) {
  78.       (void) printf("wrong number of arguments\n");
  79.       (void) printf("crc_check old_crc_file new_crc_file\n");
  80.       exit(1);
  81.    }
  82.    new_file = fopen(argv[2], "r");
  83.    old_file = fopen(argv[1], "r");
  84.  
  85.    if (new_file == NULL || old_file == NULL) {
  86.       (void) printf("can't open input files\n");
  87.       (void) printf("crc_check old_crc_file new_crc_file\n");
  88.       exit(1);
  89.    }
  90.  
  91.    get_line(new_line);
  92.    get_line(old_line);
  93.  
  94.    for (; ; ) {
  95.  
  96.       check_eof();
  97.  
  98.       /* If equal, print nothing and get new lines */
  99.  
  100.       if (strcmp(old_line, new_line) == 0) {
  101.          get_line(new_line);
  102.          get_line(old_line);
  103.          continue;
  104.       }
  105.  
  106.       /* Compare just the file names */
  107.  
  108.       new_ptr = strrchr(new_line, ' ');
  109.       old_ptr = strrchr(old_line, ' ');
  110.  
  111.       if (new_ptr == NULL || old_ptr == NULL) {
  112.          (void) printf("Error in input data\n");
  113.          exit(1);
  114.       }
  115.  
  116.       if (strcmp(old_ptr, new_ptr) == 0) {
  117.  
  118.          new_ptr = strrchr(new_line, '\t');
  119.          old_ptr = strrchr(old_line, '\t');
  120.  
  121.          if (new_ptr == NULL || old_ptr == NULL) {
  122.             (void) printf("Error in input data\n");
  123.             exit(1);
  124.          }
  125.  
  126.          /* check crc change */
  127.  
  128.          if (strncmp(new_line, old_line, 4) != 0)
  129.             if (strcmp(new_ptr, old_ptr) == 0)
  130.                (void) printf("corrupt  %s", new_line + 5);
  131.             else
  132.                (void) printf("replaced %s", new_line + 5);
  133.  
  134.  
  135.          /* check permission chenage */
  136.  
  137.          if (strncmp(new_line + 5, old_line + 5, 11) != 0)
  138.             (void) printf("permiss  %s", new_line + 5);
  139.  
  140.          /* check  owner/group */
  141.  
  142.          if (strncmp(new_line+16, old_line+16, new_ptr - new_line - 15) != 0)
  143.             (void) printf("own/grp  %s", new_line + 5);
  144.  
  145.          get_line(new_line);
  146.          get_line(old_line);
  147.          continue;
  148.       }
  149.  
  150.  
  151.       if (strcmp(old_ptr, new_ptr) < 0) {
  152.          (void) printf("removed  %s", old_line + 5);
  153.          get_line(old_line);
  154.          continue;
  155.       }
  156.  
  157.       (void) printf("added    %s", new_line + 5);
  158.       get_line(new_line);
  159.  
  160.    }
  161.  
  162. }
  163.  
  164.  
  165. get_line(string)
  166. char    *string;
  167. {
  168.    if (string == new_line)
  169.       (void) fgets(string, BUF_SIZE, new_file);
  170.    else
  171.       (void) fgets(string, BUF_SIZE, old_file);
  172.  
  173. }
  174.  
  175.  
  176. check_eof()
  177. {
  178.  
  179.    if (feof(new_file)) {
  180.  
  181.       while (!feof(old_file)) {
  182.          (void) printf("removed  %s", old_line + 5);
  183.          (void) fgets(old_line, BUF_SIZE, old_file);
  184.       }
  185.       exit(0);
  186.    } else if (feof(old_file)) {
  187.       while (!feof(new_file)) {
  188.          (void) printf("added    %s", new_line + 5);
  189.          (void) fgets(new_line, BUF_SIZE, new_file);
  190.       }
  191.       exit(0);
  192.    }
  193.  
  194. }
  195.  
  196.  
  197.  
  198.